var express = require('express');
var router = express.Router();
var url = require("url");

var { workmodel, userModel, DepartmentFilesModel, myProfileModel, fileCenterModel, fileModel,flowModel } = require('../models/models')

var jwt = require('jsonwebtoken')
var multiparty = require('multiparty');
const res = require('express/lib/response');
const { log } = require('util');
/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

// 登录
router.post('/login', async (req, res) => {
  let body = req.body
  console.log(body);
  let userdata = await userModel.find({ username: body.username, password: body.password });
  if (userdata.length != 0) {
    let token = 'Bearer ' + jwt.sign(body, 'qwerwxt', { expiresIn: 66666 })
    res.send({
      msg: '验证成功',
      token,
      userdata
    })
  } else {
    res.send({
      msg: '验证失败'
    })
  }
})

// 上传头像
router.post('/getpath', (req, res) => {
  let form = new multiparty.Form();
  form.uploadDir = 'upload'
  form.parse(req, (err, data, filename) => {
    console.log(filename);
    var path = filename.file[0].path
    res.send({ msg: '上传成功', path: path })
  })
})


// 用户注册
router.post('/zhuce', async (req, res) => {
  await userModel.create(req.body)
  res.send({
    msg: '添加成功'
  })
})

// 展示用户
router.get('/list', async (req, res) => {
  let page = parseInt(req.query.page) || 1
  let pagesize = parseInt(req.query.pagesize) || 3
  let kw = req.query.kw
  let reg = new RegExp(kw, 'i')
  let data;
  let total;
  if (kw) {
    data = await userModel.find({ username: reg }).populate('roleids').limit(pagesize).skip((page - 1) * pagesize)
    total = await userModel.find({ username: reg }).populate('roleids').count()
  } else {
    data = await userModel.find().populate('roleids').limit(pagesize).skip((page - 1) * pagesize)
    total = await userModel.find().populate('roleids').count()
  }

  res.send({
    data, total
  })
})
// 修改用户
router.put('/edituserinfo/:id', async (req, res) => {
  await userModel.updateOne({ _id: req.params.id }, { $set: req.body })
  res.send({
    msg: '修改成功'
  })
})


// 展示单条用户信息
router.get('/getuserone/:id', async (req, res) => {
  let data = await userModel.find({ _id: req.params.id })
  res.send({
    data
  })
})


// 树形控件展示数据获取
router.get('/ShowDepartmentFiles', async (req, res) => {
  data = await DepartmentFilesModel.find()
  res.send({
    data
  })
})
// 个人


// 树形控件展示数据获取
router.get('/fileApi', async (req, res) => {
  data = await fileModel.find()
  res.send({
    code: 200,
    data
  })
})

/* 
我的资料 展示信息
搜索+分页
*/
router.get('/myprofile', async (req, res) => {
  // console.log(req);
  // 分页条件的
  let pagesize = req.query.pagesize
  let currentPage = req.query.currentPage
  let total = 0

  // 根据名称进行搜索
  let name = req.query.myProfileName
  let regName = new RegExp(name)

  // 分页的
  let data = []
  data = await myProfileModel.find().limit(pagesize).skip((currentPage - 1) * pagesize)
  total = await myProfileModel.find().count()

  if (name) {
    data = await myProfileModel.find({ myProfileModel: regName }).limit(pagesize).skip((currentPage - 1) * pagesize)
    total = await myProfileModel.find({ myProfileModel: regName }).count()
  }
  res.send({
    data,
    code: 200,
    mes: "成功展示用户信息",
    total
  })
})
/* 
添加我的资料接口
*/
router.post('/addmyprofile', async (req, res) => {
  let body = req.body
  console.log(body);
  let data = await myProfileModel.create(body)
  res.send({
    data,
    code: 200,
    meg: "添加我的资料成功",
  })
})
/* 
删除我的资料接口
*/
router.delete('/delmyprofile/:id', async (req, res) => {
  let id = req.params.id
  let data = await myProfileModel.deleteOne({ _id: id })
  res.send({
    data,
    code: 200,
    mes: "我的资料删除成功"
  })
})
/* 
修改我的资料接口
*/
router.post('/editmyprofile/:id', async (req, res) => {
  let id = req.params.id
  let body = req.body
  let data = await myProfileModel.updateOne({ _id: id }, { $set: body })
  res.send({
    data,
    code: 200,
    mes: "我的资料修改成功"
  })
})



/* 
资料中心 展示信息
搜索+分页
*/
router.get('/fileCenter', async (req, res) => {
  // console.log(req);
  // 分页条件的
  let pagesize = req.query.pagesize
  let currentPage = req.query.currentPage
  let total = 0

  // 根据名称进行搜索
  let name = req.query.myProfileName
  let regName = new RegExp(name)

  // 分页的
  let data = []
  data = await fileCenterModel.find().limit(pagesize).skip((currentPage - 1) * pagesize)
  total = await fileCenterModel.find().count()

  if (name) {
    data = await fileCenterModel.find({ fileCenterModel: regName }).limit(pagesize).skip((currentPage - 1) * pagesize)
    total = await fileCenterModel.find({ fileCenterModel: regName }).count()
  }
  res.send({
    data,
    code: 200,
    mes: "成功展示用户信息",
    total
  })
})


/* 
添加资料中心接口
*/
router.post('/addFileCenter', async (req, res) => {
  let body = req.body
  console.log(body);
  let data = await fileCenterModel.create(body)
  res.send({
    data,
    code: 200,
    meg: "添加资料中心成功",
  })
})

/* 
删除资料中心接口
*/
router.delete('/delFileCenter/:id', async (req, res) => {
  let id = req.params.id
  let data = await fileCenterModel.deleteOne({ _id: id })
  res.send({
    data,
    code: 200,
    mes: "资料中心删除成功"
  })
})
/* 
修改资料中心接口
*/
router.post('/editFileCenter/:id', async (req, res) => {
  let id = req.params.id
  let body = req.body
  let data = await fileCenterModel.updateOne({ _id: id }, { $set: body })
  res.send({
    data,
    code: 200,
    mes: "资料中心修改成功"
  })
})


/*
个人
 */
// 通过状态查询流程
router.get("/flowlist", async (req, res) => {
  let data=await flowModel.find().exec()
  let pageIndex =req.query.pageIndex;
  let pageSize=req.query.pageSize;
  let flowserial=req.query.flowserial;
  let name=req.query.name;
  let flowserialAeg=new RegExp(flowserial,'i')
  let nameAeg=new RegExp(name,'i')
  let total=await flowModel.count()
  data=await flowModel.find().limit(pageSize).skip((pageIndex-1)*pageSize).exec()
  if (flowserialAeg) {
    let list=await flowModel.find({flowname:flowserialAeg}).exec()
    data=list.slice((pageIndex-1)*pageSize,pageSize*pageIndex)
    total=list.length
  }else if (nameAeg) {
    let list=await flowModel.find({name:nameAeg}).exec()
    data=list.slice((pageIndex-1)*pageSize,pageSize*pageIndex)
    total=list.length
  }
  if (flowserialAeg&&nameAeg) {
    let list=await flowModel.find({$and:[{name:nameAeg},{flowname:flowserialAeg}]}).exec()
    data=list.slice((pageIndex-1)*pageSize,pageSize*pageIndex)
    total=list.length
  }
  res.send({
    data,
    total,
  })
})
// 通过状态查询流程

// 查询计划
router.get("/getwork", async (req, res) => {
  let title = req.query.title
  let sta = req.query.sta
  let plantype = req.query.plantype
  let titlereg = new RegExp(title, 'i')
  let slist = []
  if (title) {
    slist.push({ title: titlereg })
  }
  if (sta) {
    slist.push({ sta })
  }
  if (plantype) {
    slist.push({ plantype })
  }
  console.log(slist)
  let data;
  if (slist.length >= 1) {
    data = await workmodel.find({ $and: slist })
  } else {
    data = await workmodel.find()
  }
  res.send({
    data
  })
})
// 删除计划
router.delete('/delplan/:id', async (req, res) => {
  await workmodel.deleteOne({ _id: req.params.id })
  res.send('删除成功')
})
// 修改计划
router.put('/editplan/:id', async (req, res) => {
  await workmodel.updateOne({ _id: req.params.id }, { $set: req.body })
  res.send('修改成功')
})
// 新增计划
router.post('/addplan', async (req, res) => {
  await workmodel.create(req.body)
  res.send('添加成功')
})
module.exports = router